home *** CD-ROM | disk | FTP | other *** search
/ OpenGL Superbible (2nd Edition) / OpenGL SuperBible e2.iso / book / Chap08 / TEX2D / tex2d.c < prev    next >
Encoding:
C/C++ Source or Header  |  1999-09-25  |  2.5 KB  |  115 lines

  1. /*
  2.  * 2D texturing demo from Chapter 8.
  3.  *
  4.  * Written by Michael Sweet
  5.  */
  6.  
  7. /*
  8.  * Include necessary headers.
  9.  */
  10.  
  11. #include <GL/glut.h>
  12. #include <math.h>
  13. #ifndef M_PI
  14. #  define M_PI 3.141592649
  15. #endif /* !M_PI */
  16. #include "bitmap.h"
  17.  
  18.  
  19. /*
  20.  * Globals...
  21.  */
  22.  
  23. int        Width;    /* Width of window */
  24. int        Height;   /* Height of window */
  25. BITMAPINFO *TexInfo; /* Texture bitmap information */
  26. GLubyte    *TexBits; /* Texture bitmap pixel bits */
  27.  
  28.  
  29. /*
  30.  * Functions...
  31.  */
  32.  
  33. void Redraw(void);
  34. void Resize(int width, int height);
  35.  
  36.  
  37. /*
  38.  * 'main()' - Open a window and display a textured sky.
  39.  */
  40.  
  41. int                /* O - Exit status */
  42. main(int  argc,    /* I - Number of command-line arguments */
  43.      char *argv[]) /* I - Command-line arguments */
  44.     {
  45.     glutInit(&argc, argv);
  46.     glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE | GLUT_DEPTH);
  47.     glutInitWindowSize(792, 573);
  48.     glutCreateWindow("2D Textured Tea Pot");
  49.     glutReshapeFunc(Resize);
  50.     glutDisplayFunc(Redraw);
  51.  
  52.     TexBits = LoadDIBitmap("pot.bmp", &TexInfo);
  53.  
  54.     glutMainLoop();
  55.     return (0);
  56.     }
  57.  
  58.  
  59. /*
  60.  * 'Redraw()' - Redraw the window...
  61.  */
  62.  
  63. void
  64. Redraw(void)
  65.     {
  66.     /* Define the 2D texture image. */
  67.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
  68.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
  69.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  70.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  71.  
  72.     glTexImage2D(GL_TEXTURE_2D, 0, 3, TexInfo->bmiHeader.biWidth,
  73.                  TexInfo->bmiHeader.biHeight, 0, GL_BGR_EXT,
  74.          GL_UNSIGNED_BYTE, TexBits);
  75.     glEnable(GL_TEXTURE_2D);
  76.     glEnable(GL_DEPTH_TEST);
  77.     glEnable(GL_LIGHTING);
  78.     glEnable(GL_LIGHT0);
  79.  
  80.     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  81.  
  82.     glPushMatrix();
  83.     glTranslatef(0.0, 0.0, -50.0);
  84.  
  85.     glColor3f(1.0, 1.0, 1.0);
  86.     glutSolidTeapot(10.0);
  87.  
  88.     glPopMatrix();
  89.  
  90.     glFinish();
  91.     }
  92.  
  93.  
  94. /*
  95.  * 'Resize()' - Resize the window...
  96.  */
  97.  
  98. void
  99. Resize(int width,  /* I - Width of window */
  100.        int height) /* I - Height of window */
  101.     {
  102.     /* Save the new width and height */
  103.     Width  = width;
  104.     Height = height;
  105.  
  106.     /* Reset the viewport... */
  107.     glViewport(0, 0, width, height);
  108.  
  109.     glMatrixMode(GL_PROJECTION);
  110.     glLoadIdentity();
  111.     gluPerspective(30.0, (float)width / (float)height, 0.1, 1000.0);
  112.  
  113.     glMatrixMode(GL_MODELVIEW);
  114.     }
  115.